package com.haofengsoft.scce.controller;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haofengsoft.scce.adapter.index.TopNewModel;
import com.haofengsoft.scce.adapter.index.TopNewModel.ModelTypeRes;

public class TopNewController {

	private static TopNewController instance = null;
	private Context appContext;

	public static synchronized TopNewController getInstance(Context context) {
		if (instance == null) {
			instance = new TopNewController(context);
		}
		return instance;
	}

	public TopNewController(Context appContext) {
		this.appContext = appContext;
	}

	public List<TopNewModel> gainTopNewModelList(String json) {
		List<TopNewModel> topNewList = new ArrayList<TopNewModel>();

		try {
			JSONArray newsModelArray = JSON.parseObject(json).getJSONArray("NewList");
			for (int i = 0; i < newsModelArray.size(); i++) {
				JSONObject newsModelObj = newsModelArray.getJSONObject(i);
				TopNewModel topNewModel = new TopNewModel();
				topNewModel.setContent(newsModelObj.getString("Title"));
				topNewModel
						.setModelTypeRes(ModelTypeRes.getModelTypeById(newsModelObj.containsKey("NewTypeID") ? newsModelObj
								.getIntValue("NewTypeID") : 0));
				topNewModel.setId(newsModelObj.containsKey("ID") ? newsModelObj.getIntValue("ID") : 0);
				topNewModel.setSummary(newsModelObj.getString("Summary"));
				topNewModel.setDateTime(newsModelObj.getString("StrCreateDate"));
				topNewModel.setSource(newsModelObj.getString("source"));
				topNewList.add(topNewModel);

			}
		} catch (Exception e) {
		}
		return topNewList;
	}

}
